package com.fhzn.auth.mapper;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fhzn.auth.entity.Role;
import com.fhzn.auth.web.request.RoleQueryRequest;

import java.util.List;

/**
 * @author 老顾
 * @apiNote
 * @since 2023/12/20
 */
public interface RoleMapper extends BaseMapper<Role> {

    /**
     * 根据用户昵称查询角色列表
     *
     * @param nickname 用户昵称
     * @return 角色列表
     */
    default List<Role> findRolesByNickname(String nickname) {
        LambdaQueryWrapper<Role> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.select(Role::getId, Role::getName, Role::getCode, Role::getSign,
                Role::getStatus, Role::getComment, Role::getAppCode, Role::getCreator,
                Role::getModifier, Role::getCreatedTime, Role::getUpdatedTime);
        queryWrapper.eq(Role::getStatus, 1);
        queryWrapper.inSql(Role::getId, "SELECT role_id FROM abstract_auth_user_role WHERE user_id = (SELECT id FROM abstract_auth_user WHERE nickname = '" + nickname + "')");
        return selectList(queryWrapper);
    }

    /**
     * 获取角色列表
     *
     * @param request 角色请求
     * @return 角色列表
     */
    default Page<Role> getRolesByList(RoleQueryRequest request) {
        LambdaQueryWrapper<Role> queryWrapper = Wrappers.lambdaQuery();
        if (request.getName() != null && !request.getName().isEmpty()) {
            queryWrapper.like(Role::getName, request.getName());
        }
        if (request.getCode() != null && !request.getCode().isEmpty()) {
            queryWrapper.eq(Role::getCode, request.getCode());
        }
        if (request.getStatus() != null && request.getStatus() != 0) {
            queryWrapper.eq(Role::getStatus, request.getStatus());
        }
        queryWrapper.orderByAsc(Role::getAppCode);
        queryWrapper.orderByAsc(Role::getStatus);
        return selectPage(Page.of(request.getPage(), request.getPageSize()), queryWrapper);
    }

    /**
     * 根据code获取角色信息
     *
     * @param code    code
     * @param appCode 应用code
     * @return 角色信息
     */
    default Role getRoleByCode(String code, String appCode) {
        LambdaQueryWrapper<Role> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(Role::getCode, code);
        queryWrapper.eq(Role::getAppCode, appCode);
        return selectOne(queryWrapper);
    }

    /**
     * 查询应用管理员数量
     *
     * @param appCode 应用code
     * @param sign    角色标识
     * @return 数量
     */
    default Long queryAppAdminCount(String appCode, String sign) {
        LambdaQueryWrapper<Role> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(Role::getStatus, 1);
        queryWrapper.eq(Role::getSign, sign);
        queryWrapper.eq(Role::getAppCode, appCode);
        return selectCount(queryWrapper);
    }

    /**
     * 查询可以分配的角色
     *
     * @param roleIds 角色id列表
     * @return 角色列表
     */
    default List<Role> findCanAppointRoles(List<Integer> roleIds) {
        LambdaQueryWrapper<Role> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.in(Role::getId, roleIds)
                .eq(Role::getStatus, 1)
                .orderByAsc(Role::getId);
        return selectList(queryWrapper);
    }

    /**
     * 查询所有角色
     *
     * @return 角色列表
     */
    default List<Role> queryAll() {
        LambdaQueryWrapper<Role> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(Role::getStatus, 1);
        wrapper.orderByAsc(Role::getId);
        return selectList(wrapper);
    }

    /**
     * 查询所有角色
     *
     * @param appCode 应用code
     * @return 角色列表
     */
    default List<Role> findAllRolesByAppCode(String appCode) {
        LambdaQueryWrapper<Role> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(Role::getAppCode, appCode);
        wrapper.orderByAsc(Role::getId);
        return selectList(wrapper);
    }

    /**
     * 查询角色列表
     *
     * @param appCode 应用code
     * @param sign    角色标识
     * @return 角色列表
     */
    default List<Role> queryRoleByGroup(String appCode, String sign) {
        LambdaQueryWrapper<Role> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(Role::getAppCode, appCode);
        wrapper.eq(Role::getSign, sign);
        wrapper.eq(Role::getStatus, 1);
        return selectList(wrapper);
    }

    /**
     * 根据用户昵称查询角色列表
     *
     * @param invokeAppName 调用应用
     * @return 角色列表
     */
    default List<Role> findRolesByNicknameAndAppcode(String invokeAppName) {
        LambdaQueryWrapper<Role> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(Role::getAppCode, invokeAppName);
        wrapper.eq(Role::getStatus, 1);
        return selectList(wrapper);
    }
}
